<?php

namespace app\dao\goods;

use app\dao\BaseDao;
use app\model\Goods;

class GoodsDao extends BaseDao
{
    public function getPageList($data)
    {
        $dateTime = app('DateTime');
        $query = $this->getModel();

        if (!empty($data['status']))
        {
            $query = $query->where('status', '=', $data['status']);
        }

        if (!empty($data['name']))
        {
            $query = $query->where('name', 'like', '%' . $data['name'] . '%' );
        }

        if (!empty($data['number']))
        {
            $query = $query->where('number', 'like', '%' . $data['number'] . '%');
        }

        if (!empty($data['search_start_time']))
        {
            $start_time = $dateTime->date2timestamp($data['search_start_time']);
            $query = $query->where('create_time', '>=', $start_time);
        }

        if (!empty($data['search_end_time']))
        {
            $end_time = $dateTime->date2timestamp($data['search_end_time']);
            $query = $query->where('create_time', '<=', $end_time);
        }

        return $this->pagination($query, $data['page'], $data['perPage'],);
    }

    public function deleteById($id)
    {
        $this->getModel()->destroy($id);
    }

    public function getById($id)
    {
        return $this->getModel()->find($id);
    }

    public function getAll($data)
    {
        $query = $this->getModel();
        return [
            'pagination' => ['page' => 1, 'perPage' => -1, 'total' => $query->count()],
            'list'       => $query->select()
        ];
    }

    protected function setModel(): string
    {
        return Goods::class;
    }



}